package com.csusm.geo.science.tutor;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class GeoScienceTutorAppActivity extends Activity {
	ArrayList<String> locValues = null;
	ArrayAdapter<String> adapter = null;
	String selectedFromList = null;

	// Called when the activity is first created.
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.selecteditems);
		// Show a message to the user
		Toast.makeText(getApplicationContext(), "Waiting for GPS data..",
				Toast.LENGTH_LONG).show();
		locValues = new ArrayList<String>();
		locValues.add(" ");
		// Acquire a reference to the system Location Manager
		LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		LocationListener mlocListener = new MyLocationListener();
		// Get the location from GPS provider
		mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
				110000, 100, mlocListener);

		// If there is data in locValues create a listview and assign values
		if (!locValues.isEmpty() && locValues != null) {
			final ListView listView = (ListView) findViewById(R.id.mylist);
			adapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_1, android.R.id.text1,
					locValues);

			// Assign adapter to ListView
			listView.setAdapter(adapter);
			listView.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> myAdapter, View myView,
						int myItemInt, long mylng) {
					// Get the selected location by the user
					selectedFromList = (String) (listView
							.getItemAtPosition(myItemInt));
					setContentView(R.layout.mainmenu);
				}
			});
		} else {
			Toast.makeText(getApplicationContext(), "No values found",
					Toast.LENGTH_LONG).show();
		}
		// Start the service
		Intent i = new Intent(this, TutorService.class);
		startService(i);
	}

	public void onClickVideo(View v) {
		// Call the VideoViewActivity
		Intent intent = new Intent(this, VideoViewActivity.class);
		// Send the selected location information to the Activity
		intent.putExtra("SelLoc", selectedFromList);
		// Start activity
		startActivity(intent);
	}

	public void onClickSlideshow(View v) {
		// call ImageSwitcherActivity
		Intent intent = new Intent(this, ImageSwitcherActivity.class);
		// Send the selected location information to the Activity
		intent.putExtra("SelLoc", selectedFromList);
		startActivity(intent);
	}

	public void onClickQuestion(View v) {
		// Start Drag and drop activity to start the questioner
		Intent intent = new Intent(this, DragDropActivity.class);
		// Send the selected location information to the Activity
		intent.putExtra("SelLoc", selectedFromList);
		startActivity(intent);
	}

	public void onClickQuit(View v) {
		// Stop the service already started
		stopService(new Intent(this, TutorService.class));
		finish();
	}
	// Define a listener that responds to location updates
	public class MyLocationListener implements LocationListener {

		@Override
		public void onLocationChanged(Location loc) {
			// Get the data from database for the current location
			LocationDbHelper ldbhelper = new LocationDbHelper();
			ArrayList<LocationsDb> temploc = ldbhelper.getLinks(loc);
			for (int i = 0; i < temploc.size(); i++) {
				LocationsDb ldb = new LocationsDb();
				ldb = temploc.get(i);
				locValues.add(i, ldb.getPlace() + " " + ldb.getLat() + " "
						+ ldb.getLon());
			}
			// notify change in data set
			adapter.notifyDataSetChanged();
		}

		@Override
		public void onProviderDisabled(String provider) {

			Toast.makeText(getApplicationContext(), "Gps Disabled",
					Toast.LENGTH_SHORT).show();

		}

		@Override
		public void onProviderEnabled(String provider) {

			Toast.makeText(getApplicationContext(), "Gps Enabled",
					Toast.LENGTH_SHORT).show();

		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {

		}
	}
}